<div class="content-wrapper">
	<section class="content-header">
		<h1>菜单列表</h1>
		<ol class="breadcrumb">
			<li><a href="/admin"><i class="fa fa-dashboard"></i> 首页</a></li>
			<li class="active">权限管理</li>
			<li class="active">菜单列表</li>
		</ol>
	</section>
	<section class="content">
		<div class="box">
			<div class="box-header with-border">
				<form class="form-inline" id="add_upd_id"></form>
			</div>
			<div class="box-body">
				<table class="table table-bordered">
					<tbody>
						<tr>
							<th style="width: 40px">ID</th>
							<th>一级目录</th>
							<th>二级目录</th>
							<th>功能</th>
							<th>URL</th>
							<th style="width: 215px">操作</th>
						</tr>
						<tr>
                            <td>0</td>
                            <td>顶级目录</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td><button class="btn bg-maroon btn-xs" type="button" onclick="auth.menu_add(0, 0);">添加</button></td>
                        </tr>
						<foreach name="menuList" item="menu">
                    		<tr>
                    			<td>{$menu.info.id}</td>
                    			<td>【{$menu.info.sort_id}】{$menu.info.name}</td>
                    			<td></td>
                    			<td><php>echo $functionList[$menu['info']['function_id']]['name']</php></td>
                    			<td><a href="<php>echo $functionList[$menu['info']['function_id']]['path'].$menu['info']['url_extend'];</php>"><php>echo $functionList[$menu['info']['function_id']]['path'].$menu['info']['url_extend'];</php></a></td>
                    			<td>
                    				<button class="btn bg-maroon btn-xs" type="button" onclick="auth.menu_add({$menu.info.id},{$menu.info.id});">添加</button>&nbsp;		                   			
	                  				<php>$menu_info = $menu['info']['id'] . "," . $menu['info']['pid'] . ",'" . $menu['info']['name'] . "','" . $menu['info']['url_extend'] . "'," . $menu['info']['function_id'] . "," . $menu['info']['sort_id'] . ",'" . $menu['info']['icon'] . "'";</php>
	                  				<button class="btn btn-primary btn-xs" type="button" onclick="auth.menu_upd({$menu_info});">修改</button>&nbsp;
	                  				<button class="btn btn-danger btn-xs" type="button" onclick="auth.menu_del({$menu.info.id});">删除</button>&nbsp;	                  				
                    			</td>
                    		</tr>
                    		<foreach name="menu.next" item="sub_menu">
                    			<tr>
	                    			<td>{$sub_menu.info.id}</td>
	                    			<td></td>
	                    			<td>【{$sub_menu.info.sort_id}】{$sub_menu.info.name}</td>
	                    			<td><php>echo $functionList[$sub_menu['info']['function_id']]['name']</php></td>
	                    			<td><a href="<php>echo $functionList[$sub_menu['info']['function_id']]['path'].$sub_menu['info']['url_extend'];</php>"><php>echo $functionList[$sub_menu['info']['function_id']]['path'].$sub_menu['info']['url_extend'];</php></a></td>
	                    			<td>
	                    				<button class="btn bg-maroon btn-xs" type="button" onclick="auth.menu_add({$sub_menu.info.pid},{$sub_menu.info.id});">添加</button>&nbsp;	                    				
	                    				<php>$menu_info = $sub_menu['info']['id'] . "," . $sub_menu['info']['pid'] . ",'" . $sub_menu['info']['name'] . "','" . $sub_menu['info']['url_extend'] . "'," . $sub_menu['info']['function_id'] . "," . $sub_menu['info']['sort_id'] . ",'" . $sub_menu['info']['icon'] . "'";</php>
	                    				<button class="btn btn-primary btn-xs" type="button" onclick="auth.menu_upd({$menu_info});">修改</button>&nbsp;
	                    				<button class="btn btn-danger btn-xs" type="button" onclick="auth.menu_del({$sub_menu.info.id});">删除</button>&nbsp;	                    				
	                    			</td>
	                    		</tr>
                    		</foreach>
                    	</foreach>
					</tbody>
				</table>
			</div>			
		</div>
	</section>
</div>
<script type="text/javascript">
var auth = {
    menu_add: function(pid, preid) {
    	var html = [];
    	html.push('<div class="form-group form-group-sm">');
    	html.push('<label for="menu_name">目录名&nbsp;</label>');
    	html.push('<input type="text" class="form-control" id="menu_name" name="menu_name" placeholder="目录名">&nbsp;');
    	html.push('</div>');
    	
    	html.push('<div class="form-group form-group-sm">');
    	html.push('<label for="func_id">功能ID&nbsp;</label>');
    	html.push('<input type="text" class="form-control" id="func_id" name="func_id" placeholder="功能ID">&nbsp;');
    	html.push('</div>');
    	
    	html.push('<div class="form-group form-group-sm">');
    	html.push('<label for="url_extend">URL扩展&nbsp;</label>');
    	html.push('<input type="text" class="form-control" id="url_extend" name="url_extend" placeholder="URL扩展">&nbsp;');
    	html.push('</div>');
    	
    	if(pid == 0) {
    		html.push('<div class="form-group form-group-sm">');
        	html.push('<label for="icon">图标&nbsp;</label>');
        	html.push('<input type="text" class="form-control" id="icon" name="icon" placeholder="菜单图标">&nbsp;');
        	html.push('</div>');
    	}
    	
    	html.push('<button type="button" class="btn btn-info btn-sm" onclick="auth.menu_add_do(' + pid + ',' + preid + ')">添加</button>&nbsp;');
    	html.push('<button type="button" class="btn btn-danger btn-sm" onclick="$(\'#add_upd_id\').empty().html(\'\')">取消</button>');
    	        
        $("#add_upd_id").html(html.join(""));
    },
    menu_upd: function(menu_id,pid, menu_name, url_extend, func_id, sort_id, icon) {
    	var html = [];
    	html.push('<div class="form-group form-group-sm">');
    	html.push('<label for="menu_name">目录名&nbsp;</label>');
    	html.push('<input type="text" class="form-control" id="menu_name" name="menu_name" placeholder="目录名" value="' + menu_name + '">&nbsp;');
    	html.push('</div>');
    	
    	html.push('<div class="form-group form-group-sm">');
    	html.push('<label for="func_id">功能ID&nbsp;</label>');
    	html.push('<input type="text" class="form-control" id="func_id" name="func_id" placeholder="功能ID" value="' + func_id + '">&nbsp;');
    	html.push('</div>');
    	
    	html.push('<div class="form-group form-group-sm">');
    	html.push('<label for="url_extend">URL扩展&nbsp;</label>');
    	html.push('<input type="text" class="form-control" id="url_extend" name="url_extend" placeholder="URL扩展" value="' + url_extend + '">&nbsp;');
    	html.push('</div>');
    	
    	html.push('<div class="form-group form-group-sm">');
    	html.push('<label for="sort_id">排序ID&nbsp;</label>');
    	html.push('<input type="text" class="form-control" id="sort_id" name="sort_id" placeholder="值越小越靠前" value="' + sort_id + '">&nbsp;');
    	html.push('</div>');
    	
    	if(pid == 0) {
    		html.push('<div class="form-group form-group-sm">');
        	html.push('<label for="icon">图标&nbsp;</label>');
        	html.push('<input type="text" class="form-control" id="icon" name="icon" placeholder="菜单图标" value="' + icon + '">&nbsp;');
        	html.push('</div>');
    	}
    	
    	html.push('<button type="button" id="menu-upd-btn" class="btn btn-info btn-sm" onclick="auth.menu_upd_do(\'' + menu_id + '\')">修改</button>&nbsp;');
    	html.push('<button type="button" class="btn btn-danger btn-sm" onclick="$(\'#add_upd_id\').empty().html(\'\')">取消</button>');    	 
    	
        $("#add_upd_id").html(html.join(""));
    },
    menu_add_do: function(pid, preid) {
        var menu_name = $("#menu_name").val();
        var url_extend = $("#url_extend").val();
        var func_id = $("#func_id").val();
        var icon = $("#icon").val();
        var data = {pid: pid, url_extend: url_extend, preid: preid, menu_name: menu_name, func_id: func_id, icon : icon};
        $.post("/admin/auth/addMenu", data, function(_data) {
        	if(_data.Code == 999) {
        		ArtDialogHlp.showInfo('提示', '操作成功', function(){
					window.location.href = '/admin/auth/menuList';
				});
			} else {
				ArtDialogHlp.showInfo('提示', _data.Msg);
			}
        },'json');
    },
    menu_upd_do: function(menu_id) {
        var menu_name = $("#menu_name").val();
        var url_extend = $("#url_extend").val();
        var func_id = $("#func_id").val();
        var sort_id = $("#sort_id").val();
        var icon = $("#icon").val();
        var data = {sort_id: sort_id, url_extend: url_extend, id: menu_id, menu_name: menu_name, func_id: func_id, icon : icon};
        $.post("/admin/auth/updateMenu", data, function(_data) {
        	if(_data.Code == 999) {
        		ArtDialogHlp.showInfo('提示', '操作成功', function(){
					window.location.href = '/admin/auth/menuList';
				});
			} else {
				ArtDialogHlp.showInfo('提示', _data.Msg);
			}
        },'json');
    },
    menu_del: function(menu_id) {
    	ArtDialogHlp.showConfirm('提示', '确认删除?', function(){
    		var data = {id: menu_id};
            $.post("/admin/auth/deleteMenu", data, function(_data) {
            	if(_data.Code == 999) {
            		ArtDialogHlp.showInfo('提示', '操作成功', function(){
    					window.location.href = '/admin/auth/menuList';
    				});
    			} else {
    				ArtDialogHlp.showInfo('提示', _data.Msg);
    			}
            },'json');
		});
    },
}
</script>